/*  ###################################################################
 **     Filename  : KwikStik_SegLCD.c
 **     Project   : KwikStik Swing Counter Watch demo
 **     Target    : KwikStik V.5
 **     Author    : mvinkler
 **     Abstract  :
 **         This is module contains high level driver routines for KwikStik segment LCD.
 **
 ** ###################################################################*/

#include "PE_LDD.h"
#include "KwikStik_SegLCD.h"


/* Character table */
static const uint8_t CharTable[][5] = {
		{0x00,0x1C,0x1C,0x1C,0x00}, //0
		{0x00,0x1C,0x1C,0x1C,0x00}, //1
		{0x00,0x1C,0x1C,0x1C,0x00}, //2
		{0x00,0x1C,0x1C,0x1C,0x00}, //3
		{0x00,0x1C,0x1C,0x1C,0x00}, //4
		{0x00,0x1C,0x1C,0x1C,0x00}, //5
		{0x00,0x1C,0x1C,0x1C,0x00}, //6
		{0x00,0x1C,0x1C,0x1C,0x00}, //7
		{0x00,0x1C,0x1C,0x1C,0x00}, //8
		{0x00,0x1C,0x1C,0x1C,0x00}, //9
		{0x00,0x1C,0x1C,0x1C,0x00}, //10 
		{0x00,0x1C,0x1C,0x1C,0x00}, //11
		{0x00,0x1C,0x1C,0x1C,0x00}, //12
		{0x00,0x1C,0x1C,0x1C,0x00}, //13
		{0x00,0x1C,0x1C,0x1C,0x00}, //14
		{0x00,0x1C,0x1C,0x1C,0x00}, //15
		{0x00,0x1C,0x1C,0x1C,0x00}, //16
		{0x00,0x1C,0x1C,0x1C,0x00}, //17
		{0x00,0x1C,0x1C,0x1C,0x00}, //18
		{0x00,0x1C,0x1C,0x1C,0x00}, //19
		{0x00,0x1C,0x1C,0x1C,0x00}, //20
		{0x00,0x1C,0x1C,0x1C,0x00}, //21
		{0x00,0x1C,0x1C,0x1C,0x00}, //22
		{0x00,0x1C,0x1C,0x1C,0x00}, //23
		{0x00,0x1C,0x1C,0x1C,0x00}, //24
		{0x00,0x1C,0x1C,0x1C,0x00}, //25
		{0x00,0x1C,0x1C,0x1C,0x00}, //26
		{0x00,0x1C,0x1C,0x1C,0x00}, //27
		{0x00,0x1C,0x1C,0x1C,0x00}, //28 
		{0x00,0x1C,0x1C,0x1C,0x00}, //29
		{0x00,0x1C,0x1C,0x1C,0x00}, //30
		{0x00,0x1C,0x1C,0x1C,0x00}, //31
		{0x00,0x00,0x00,0x00,0x00}, // space
		{0x5F,0x00,0x00,0x00,0x00}, //'!'
		{0x00,0x06,0x00,0x06,0x00}, //'"'
		{0x14,0x3E,0x14,0x3E,0x14}, //'#'
		{0x24,0x2A,0x7F,0x2A,0x12}, //'$'
		{0x26,0x16,0x08,0x34,0x32}, //'%'
		{0x00,0x1C,0x1C,0x1C,0x00}, //38
		{0x00,0x00,0x07,0x00,0x00}, //'''
		{0x00,0x1C,0x22,0x41,0x00}, //'('
		{0x00,0x41,0x22,0x1C,0x00}, //')'
		{0x2A,0x1C,0x3E,0x1C,0x2A}, //'*'
		{0x00,0x08,0x1C,0x08,0x00}, //'+'
		{0x60,0x00,0x00,0x00,0x00}, //','
		{0x00,0x08,0x08,0x08,0x00}, //'-'
		{0x40,0x00,0x00,0x00,0x00}, //'.'
		{0x20,0x10,0x08,0x04,0x02}, //'/'
		{0xFF,0x81,0x81,0xFF,0x00}, //'0'
		{0x00,0x00,0x00,0xFF,0x00}, //'1'
		{0xF9,0x89,0x89,0x8F,0x00}, //'2'
		{0x89,0x89,0x89,0xFF,0x00}, //'3'
		{0x0F,0x08,0xFC,0x08,0x00}, //'4'
		{0x8F,0x89,0x89,0xF9,0x00}, //'5'
		{0xFF,0x89,0x89,0xF9,0x00}, //'6'
		{0x01,0x01,0x01,0xFF,0x00}, //'7'
		{0xFF,0x89,0x89,0xFF,0x00}, //'8'
		{0x8F,0x89,0x89,0xFF,0x00}, //'9'
		{0x6C,0x00,0x00,0x00,0x00}, //':'   
		{0x00,0x1C,0x1C,0x1C,0x00}, //59
		{0x00,0x08,0x14,0x22,0x00}, //'<'
		{0x00,0x14,0x14,0x14,0x00}, //'='
		{0x00,0x22,0x14,0x08,0x00}, //'>'
		{0x02,0x01,0x51,0x09,0x06}, //'?'
		{0x3E,0x41,0x5D,0x5D,0x06}, //'@'   
		{0x7E,0x11,0x11,0x11,0x7E}, //'A'
		{0x7F,0x49,0x49,0x49,0x36}, //'B'
		{0x3E,0x41,0x41,0x41,0x22}, //'C'
		{0x7F,0x41,0x41,0x41,0x3E}, //'D'
		{0x7F,0x49,0x49,0x49,0x41}, //'E'
		{0x7F,0x09,0x09,0x09,0x01}, //'F'
		{0x3E,0x41,0x49,0x49,0x3A}, //'G'
		{0x7F,0x08,0x08,0x08,0x7F}, //'H'
		{0x00,0x00,0x7F,0x00,0x00}, //'I'
		{0x21,0x41,0x41,0x41,0x3F}, //'J'
		{0x7F,0x08,0x14,0x22,0x41}, //'K'
		{0x7F,0x40,0x40,0x40,0x40}, //'L'
		{0x7F,0x02,0x04,0x02,0x7F}, //'M'
		{0x7F,0x04,0x08,0x10,0x7F}, //'N'     
		{0x3E,0x41,0x41,0x41,0x3E}, //'O'
		{0x7F,0x09,0x09,0x09,0x06}, //'P'
		{0x3E,0x41,0x51,0x21,0x5E}, //'Q'
		{0x7F,0x09,0x19,0x29,0x46}, //'R'
		{0x26,0x49,0x49,0x49,0x32}, //'S'
		{0x01,0x01,0x7F,0x01,0x01}, //'T'
		{0x3F,0x40,0x40,0x40,0x3F}, //'U'
		{0x1F,0x20,0x40,0x20,0x1F}, //'V'    
		{0x3F,0x40,0x30,0x40,0x3F}, //'W'
		{0x63,0x14,0x08,0x14,0x63}, //'X'    
		{0x03,0x0C,0x70,0x0C,0x03}, //'Y'    
		{0x61,0x51,0x49,0x45,0x43}, //'Z'    
		{0x00,0x7F,0x41,0x41,0x00}, //'['
		{0x02,0x04,0x08,0x10,0x20}, //'\'
		{0x00,0x41,0x41,0x7F,0x00}, //']'
		{0x00,0x04,0x02,0x04,0x00}, //'^'
		{0x80,0x80,0x80,0x80,0x80},  //'_'
		{0x00,0x00,0x02,0x04,0x00}, //'`'
		{0x7E,0x11,0x11,0x11,0x7E}, //'A'
		{0x7F,0x49,0x49,0x49,0x36}, //'B'
		{0x3E,0x41,0x41,0x41,0x22}, //'C'
		{0x7F,0x41,0x41,0x41,0x3E}, //'D'
		{0x7F,0x49,0x49,0x49,0x41}, //'E'
		{0x7F,0x09,0x09,0x09,0x01}, //'F'
		{0x3E,0x41,0x49,0x49,0x3A}, //'G'
		{0x7F,0x08,0x08,0x08,0x7F}, //'H'
		{0x00,0x00,0x7F,0x00,0x00}, //'I'
		{0x21,0x41,0x41,0x41,0x3F}, //'J'
		{0x7F,0x08,0x14,0x22,0x41}, //'K'
		{0x7F,0x40,0x40,0x40,0x40}, //'L'
		{0x7F,0x02,0x04,0x02,0x7F}, //'M'
		{0x7F,0x06,0x08,0x30,0x7F}, //'N'     
		{0x3E,0x41,0x41,0x41,0x3E}, //'O'
		{0x7F,0x09,0x09,0x09,0x06}, //'P'
		{0x3E,0x41,0x51,0x21,0x5E}, //'Q'
		{0x7F,0x09,0x19,0x29,0x46}, //'R'
		{0x26,0x49,0x49,0x49,0x32}, //'S'
		{0x01,0x01,0x7F,0x01,0x01}, //'T'
		{0x3F,0x40,0x40,0x40,0x3F}, //'U'
		{0x1F,0x20,0x40,0x20,0x1F}, //'V'    
		{0x3F,0x40,0x30,0x40,0x3F}, //'W'
		{0x63,0x14,0x08,0x14,0x63}, //'X'    
		{0x03,0x0C,0x70,0x0C,0x03}, //'Y'    
		{0x61,0x51,0x49,0x45,0x43}, //'Z' 
		{0x00,0x08,0x36,0x41,0x00}, //'{'
		{0x00,0x00,0x3E,0x00,0x00}, //'|'
		{0x00,0x41,0x36,0x08,0x00}, //'}'
		{0x03,0x03,0x03,0x03,0x00}, //'~'
		{0x00,0x1C,0x1C,0x1C,0x00}, //127     
};

/*
 ** ====================================================================================
 ** Displays one character on segment LCD at position specified by parameter posX  
 ** ====================================================================================
 */
void lcd_DisplayChar(LDD_TDeviceData *lcdDevicePtr, int16_t posX, char ch)
{
	uint8_t i, chIdx;
	int16_t fPlaneIdx;

	chIdx = (uint8_t)ch;	
	/* Update appropriate LCD front planes */
	for (i = 0; i < 6; i++) {
		fPlaneIdx = posX + i + 1; 
		if ((fPlaneIdx > 0) && (fPlaneIdx < 38)) {
			if (i < 5) {  
				SegLCD1_SetFrontplaneData(lcdDevicePtr, (LDD_SegLCD_TPinIndex)fPlaneIdx, CharTable[chIdx][i]);
			} else {
				SegLCD1_SetFrontplaneData(lcdDevicePtr, (LDD_SegLCD_TPinIndex)fPlaneIdx, 0);
			}
		}	
	}		  
}


/*
 ** ====================================================================================
 ** Displays string on segment LCD at position specified by parameter posX  
 ** ====================================================================================
 */
void lcd_DisplayString(LDD_TDeviceData *lcdDevicePtr, int16_t posX, char *str)
{
	uint8_t i = 0;
	int16_t charPosX = posX;

	while (str[i] != '\0') {
		lcd_DisplayChar(lcdDevicePtr, charPosX, str[i]);
		if ((str[i] == ':') || (str[i] == '.')) {
			charPosX += 2;
		} else if ((str[i] >= '0') && (str[i] <= '9')) {
			charPosX += 5;
		} else {    
			charPosX += 6;
		}
		i++;
	}  
}


/*
 ** ====================================================================================
 ** Enables/Disables the LCD graphical segment specified by parameter lcdSegment 
 ** ====================================================================================
 */
void lcd_SetGraphSegment(LDD_TDeviceData *lcdDevicePtr, TSegLcdGraphSegment lcdSegment, bool state)
{
	LDD_SegLCD_TFrontplaneData  tmpFpData;

	SegLCD1_GetFrontplaneData(lcdDevicePtr, 0, &tmpFpData);	
	if (state) {
		SegLCD1_SetFrontplaneData(lcdDevicePtr, 0, tmpFpData | (LDD_SegLCD_TFrontplaneData)lcdSegment);
	} else {
		SegLCD1_SetFrontplaneData(lcdDevicePtr, 0, tmpFpData & ~((LDD_SegLCD_TFrontplaneData)lcdSegment));
	}	
}

/*
 ** ====================================================================================
 ** Gets status of the LCD graphical segment specified by parameter lcdSegment 
 ** ====================================================================================
 */
bool lcd_GetGraphSegment(LDD_TDeviceData *lcdDevicePtr, TSegLcdGraphSegment lcdSegment)
{
	LDD_SegLCD_TFrontplaneData  tmpFpData;

	SegLCD1_GetFrontplaneData(lcdDevicePtr, 0, &tmpFpData);
	return (tmpFpData & (LDD_SegLCD_TFrontplaneData)lcdSegment);
}

/*
 ** ====================================================================================
 ** Clears the segment LCD text area 
 ** ====================================================================================
 */
void lcd_ClearScreen(LDD_TDeviceData *lcdDevicePtr)
{
	uint8_t i;

	for (i = 1; i <= 38; i++) {
		SegLCD1_SetFrontplaneData(lcdDevicePtr, i, 0);
	}		  
}


